package com.chensf.studydemo.common

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.chensf.studydemo.R
import kotlinx.android.synthetic.main.fragment_calendar_test.*
import java.text.SimpleDateFormat
import java.util.*

class CalendarTest : Fragment() {
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_calendar_test, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        button7.setOnClickListener {
            test1()
        }

        button8.setOnClickListener {
            test2()
        }
    }

    private fun test2() {
        val format = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
        val date = format.parse("2020-10-07 15:20:00")
        val calendar = Calendar.getInstance(Locale.getDefault())
        val currentDay = calendar.get(Calendar.YEAR)*10000+calendar.get(Calendar.MONTH)*100+calendar.get(Calendar.DATE)
        calendar.timeInMillis = date.time
        val weekday = calendar.get(Calendar.DAY_OF_WEEK)-1
        calendar.add(Calendar.DATE,0-weekday)
        val startDay = calendar.get(Calendar.YEAR)*10000+calendar.get(Calendar.MONTH)*100+calendar.get(Calendar.DATE)
        val dateStr = format.format(calendar.timeInMillis)
        Log.e("foo", "日期=$dateStr")
        calendar.add(Calendar.DATE,6)
        val endDay = calendar.get(Calendar.YEAR)*10000+calendar.get(Calendar.MONTH)*100+calendar.get(Calendar.DATE)
        Log.e("foo", "日期=${format.format(calendar.timeInMillis)}")


    }

    private fun test1() {
        val startTime = 1585450533L
        val endTime = 1601348133L
        val calendar1 = Calendar.getInstance()
        calendar1.time = Date(startTime * 1000)
        val calendar2 = Calendar.getInstance()
        calendar2.time = Date(endTime * 1000)
        Log.e("foo", "calendar=$calendar1")
        Log.e("foo", "calendar=$calendar2")

        //先判断开始和结束时间是不是一天
        var startYear = calendar1.get(Calendar.YEAR)
        var startMonth = calendar1.get(Calendar.MONTH)
        var startDate = calendar1.get(Calendar.DAY_OF_MONTH)

        val endYear = calendar2.get(Calendar.YEAR)
        val endMonth = calendar2.get(Calendar.MONTH)
        val endDate = calendar2.get(Calendar.DAY_OF_MONTH)
        val list = mutableListOf<String>()
        //不是同一天
        while (startYear != endYear || startMonth != endMonth || startDate != endDate) {
            //加一天
            list.add("$startYear-${startMonth + 1}-$startDate")
            calendar1.add(Calendar.DATE, 1)
            startYear = calendar1.get(Calendar.YEAR)
            startMonth = calendar1.get(Calendar.MONTH)
            startDate = calendar1.get(Calendar.DAY_OF_MONTH)
        }
        list.add("$startYear-${startMonth + 1}-$startDate")
        list.forEach {
            Log.e("foo", "日期=$it")
        }

    }
}